package com.telecom.weixi.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.telecom.weixi.R;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TermInfoAdapter extends BaseAdapter {
	public static final String Tag_Term_Portrait = "cust_portait";	//Drawable
	public static final String Tag_Term_Phone = "cust_phone";		//String
	public static final String Tag_Term_Info = "term_info";			//String
	public static final String Tag_Term_Info2 = "term_info2";			//String
	public static final String Tag_Next_Arrow = "next_arrow";	//Drawable		
	List<Map<String, Object>> custInfo;
	LayoutInflater flater;
	boolean mIsExtra;
	
	public TermInfoAdapter(Context context){
		this(context, false);
	}
	public TermInfoAdapter(Context context, boolean isExtra){
		custInfo = new ArrayList<Map<String, Object>>();
		flater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mIsExtra = isExtra;
	}
	
	public void addItem(Map<String, Object> item){
		custInfo.add(item);
	}
	
	public void clearData(){
		custInfo.clear();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return custInfo.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return custInfo.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;
		if(convertView != null){
			holder = (ViewHolder) convertView.getTag();
		}else{
			holder = new ViewHolder();
			convertView = flater.inflate(R.layout.item_term_info, null);
			holder.portrait = (ImageView) convertView.findViewById(R.id.term_portrait);
			holder.container = (LinearLayout) convertView.findViewById(R.id.term_container);
			holder.phone = (TextView) convertView.findViewById(R.id.term_phone);
			holder.termNum = (TextView) convertView.findViewById(R.id.term_num);
			holder.infoExtra = (TextView) convertView.findViewById(R.id.info_extra);
			holder.nextArrow = (ImageView) convertView.findViewById(R.id.term_arrow);
			convertView.setTag(holder);
		}
		
		Map<String, Object> item = custInfo.get(position);
		Drawable portrait = (Drawable) item.get(Tag_Term_Portrait);
		if(portrait != null){
			holder.portrait.setBackgroundDrawable(portrait);
		}
		holder.phone.setText((CharSequence) item.get(Tag_Term_Phone));
		holder.termNum.setText((CharSequence) item.get(Tag_Term_Info));
		if(mIsExtra){
			holder.infoExtra.setVisibility(View.VISIBLE);
			holder.infoExtra.setText((CharSequence) item.get(Tag_Term_Info2));
		}else{
			holder.infoExtra.setVisibility(View.GONE);
		}
		Drawable nextArrow = (Drawable) item.get(Tag_Next_Arrow);
		if(nextArrow != null){
			holder.nextArrow.setBackgroundDrawable(nextArrow);
		}
		
		return convertView;
	}
	
	class ViewHolder{
		ImageView portrait;
		LinearLayout container;
		TextView phone;
		TextView termNum;
		TextView infoExtra;
		ImageView nextArrow;
	}

}
